package cn.swust.designpatternlearningplatform.controller;

import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.AnnotationUtils.NotNull;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.domain.dto.ClassDto;
import cn.swust.designpatternlearningplatform.domain.bo.StudentSimplyInformationBo;
import cn.swust.designpatternlearningplatform.domain.dto.StudentSimplyInformationDto;
import cn.swust.designpatternlearningplatform.domain.enums.RoleEnum;
import cn.swust.designpatternlearningplatform.service.ClassManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/class")
@Authentication(RoleEnum.TEACHER)
@Slf4j
public class ClassManagementController {
    @Resource
    private ClassManagementService classManagementService;

    /**
     * @return R<Object>
     * @author wang
     * description 返回班级学期列表
     * date 2023/4/24 10:29
     */
    @RequestMapping("/getSemester")
    public R<Object> GetSemester() {
        String teacherAccount = AuthUserContext.get().getNumber();
        Set<String> list = classManagementService.getSemester(teacherAccount);
        return R.success(list);
    }

    /**
     * @param semester:
     * @return R<Object>
     * @author wang
     * description 通过学期返回班级号码以及人数
     * date 2023/4/24 11:06
     */
    @RequestMapping("/getClass")
    @NotNull
    public R<Object> getClass(String semester) {
        String teacherAccount = AuthUserContext.get().getNumber();
        List<ClassDto> list = classManagementService.getClassNumber(semester, teacherAccount);
        return R.success(list);
    }

    /**
     * @param classNumber:
     * @param currentPage:
     * @param pageSize:
     * @return R<Object>
     * @author wang
     * description 分页获取学生的信息
     * date 2023/4/26 10:32
     */
    @RequestMapping("/getStudentInformation")
    public R<Object> GetStudentInformation(String classNumber, Integer currentPage, Integer pageSize, String semester) {
        String teacherAccount = AuthUserContext.get().getNumber();
        if (currentPage==null||pageSize==null||semester.isEmpty())
        {
            return R.error(201,"参数有误，查询失败");
        }
        StudentSimplyInformationDto studentInformation = classManagementService.getStudentInformation(classNumber, currentPage, pageSize, semester, teacherAccount);
        if (studentInformation==null)
        {
            return R.success("请求无数据");
        }
        else{
            return R.success(studentInformation);
        }
    }


    /**
     * @param multipartFile:
     * @return R<String>
     * @author wang
     * description 增加学生
     * date 2023/4/27 14:25
     */
    @RequestMapping("/addStudents")
    public R<String> AddStudent(@RequestPart("students") MultipartFile multipartFile, String semester) throws IOException {
        if(semester.isEmpty())
        {
            return R.success("参数有误！");
        }
        String name = multipartFile.getOriginalFilename();

        String teacherAccount = AuthUserContext.get().getNumber();

        if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
            return R.error("文件格式错误");
        }
        classManagementService.AddStudent(multipartFile, teacherAccount, semester);
        return R.success("上传成功");
    }

    @RequestMapping("/deleteClass")
    public R<String> DeleteClass(String semester, String classNumber) {
        if (semester.isEmpty()||classNumber.isEmpty())
        {
            return R.error(201,"参数有误，删除失败");
        }
        String teacherAccount = AuthUserContext.get().getNumber();
        classManagementService.DeleteClass(semester, classNumber, teacherAccount);
        return R.success("删除成功");
    }

    @RequestMapping("/deleteStudent")
    public R<String> DeleteStudent(@RequestBody List<String> studentNumbers) {
        classManagementService.DeleteStudents(studentNumbers);
        return R.success("删除成功");
    }

}
